<?php
class MiscUtils {
  public static function dump($obj, $what = NULL) {
    if (defined ('DEBUG_ENABLED') && ! DEBUG_ENABLED) {
      return;
    }
    echo "<pre>\n";
    if (is_null ($obj)) {
      echo ($what !== NULL) ? "$what=!NULL!" : '!NULL!';
    } else if ($obj === '') {
      echo ($what !== NULL) ? "$what=!EMPTY!" : '!EMPTY!';
    } else if ($obj === false) {
      echo ($what !== NULL) ? "$what=!FALSE!" : '!FALSE!';
    } else {
      if ($what !== NULL) {
        echo "$what=<br>";
      }
      echo htmlspecialchars (print_r ($obj, true));
    }
    echo "</pre>\n";
  }
  
  public static function getParam($itm, $default = '', $trimText = false) {
    if (isset($_POST[$itm])) {
      return ($trimText) ? trim($_POST[$itm]) : $_POST[$itm];
    } else if (isset($_GET[$itm])) {
      return ($trimText) ? trim($_GET[$itm]) : $_GET[$itm];
    } else {
      return $default;
    }
  }
  
  public static function decryptParam($itm, $default = '', $trimText = false) {
    return MiscUtils::decrypt(MiscUtils::getParam($itm, $default = '', $trimText = false));
  }
  
  public static function digits($value, $size) {
    $prefix = '';
    for ($i = 0, $il = $size - strlen($value); $i < $il; $i++) {
      $prefix .= '0';
    }
    return $prefix . $value;
  }
  
  public static function encrypt($str) {
    if (MODE == MODE_PUBLIC) {
      $result = '';
      for ($i = 0, $il = strlen($str); $i < $il; $i++) {
        $char = substr($str, $i, 1);
        $result .= MiscUtils::digits(dechex(ord($char)), 2);
      }
      return $result;
    } else {
      return $str;
    }
  }
  
  public static function decrypt($str) {
    if (MODE == MODE_PUBLIC) {
      $result = '';
      for ($i = 0, $il = strlen($str); $i < $il; $i += 2) {
        $ascii = substr($str, $i, 2);
        $result .= chr(hexdec($ascii));
      }
      return $result;
    } else {
      return $str;
    }
  }
  
  public static function encode($obj) {
    return json_encode($obj);
  }
  
  public static function decode($str) {
    return json_decode($str);
  }
  
  public static function unique() {
    $unique = 'u' . (mktime() * rand());
    return $unique;
  }
  
  public static function db($myPdo, $select, $table, $joins, $condition, $order, $queue, $page, $size) {
    $sql = "SELECT " . (($select) ? $select : '* ') . " FROM $table AS t ";
    if (count($joins) > 0) {
      $count = 1;
      foreach ($joins as &$join) {
        $sql .= "LEFT JOIN $join[0] AS t$count ON $join[1] ";
        $count++;
      }
    }
    $sql .= (($condition) ? "WHERE $condition " : '') . (($order && $queue) ? "ORDER BY $order $queue " : '') . (($page && $size) ? " LIMIT " . ($page - 1) * $size . ", $size" : '');
    
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    
    $data = array();
    $i = 0;
    while ($i < $stmt->rowCount()) {
      $data[] = $stmt->fetch(PDO::FETCH_OBJ);
      $i++;
    }
    
    $result = new stdClass();
    $result->data = $data;
    $result->condition = $condition;
    $result->order = $order;
    $result->queue = $queue;
    $result->page = $page;
    $result->size = $size;
    if ($page && $size) {
      $sql = substr($sql, 0, strrpos($sql, 'LIMIT'));
      $stmt = $myPdo->prepare($sql);
      $stmt->execute();
      $result->total = $stmt->rowCount();
    } else {
      $result->total = $stmt->rowCount();
    }
    return $result;
  }
}
?>